﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DAOKhuyenMai:DataProvider
    {
        public static DataTable getAll()
        {
            string select = "select * from khuyenmai";
            var da = new SqlDataAdapter(select, KetNoi());
            DataTable dt = new DataTable();
            da.Fill(dt);
            DongKetNoi();
            return dt;
        }
        
        public static int edit(DTOKhuyenMai obj)
        {
            string select = "select * from khuyenmai";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            da.FillSchema(dt, SchemaType.Source);
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                row.BeginEdit();
                row["DiemCong"] = obj.DiemCong;
                row["DiemGioiHan"] = obj.DiemGioiHan;
                row["SoTienDuocGiam"] = obj.SoTienDuocGiam;
                row["TongGiaTri"] = obj.TongGiaTri;
                row.EndEdit();                
            }
            else
            {
                DataRow row = dt.NewRow();
                row["DiemCong"] = obj.DiemCong;
                row["DiemGioiHan"] = obj.DiemGioiHan;
                row["SoTienDuocGiam"] = obj.SoTienDuocGiam;
                row["TongGiaTri"] = obj.TongGiaTri;
                dt.Rows.Add(row);
            }

            DongKetNoi();
            return da.Update(dt);
        }        
    }
}
